//
//  SearchImageView.m
//  lvtu
//
//  Created by Apple on 12-3-16.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "SearchImageView.h"
#import <QuartzCore/QuartzCore.h>

@implementation SearchImageView
@synthesize delegate;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        
        UIImageView* backgroudImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
        backgroudImageView.image = [UIImage imageNamed:@"bg_system_title.png"];
        [self addSubview:backgroudImageView];
        [backgroudImageView release];
        
        contentTextField = [[UITextField alloc] init];
        UIImage* TextFieldImage = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"bg_search.png" ofType:nil]];
        [contentTextField setFrame:CGRectMake(8, 8, TextFieldImage.size.width-10, TextFieldImage.size.height)];
        [contentTextField setBackground:TextFieldImage];
        contentTextField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;                
        [contentTextField setAutocorrectionType:UITextAutocorrectionTypeDefault];
        [contentTextField setReturnKeyType:UIReturnKeySearch];
        [contentTextField setFont:[UIFont boldSystemFontOfSize:15]];
        [contentTextField setDelegate:self];
        [contentTextField setTextColor:[UIColor grayColor]];
        //[contentTextField setPlaceholder:@"请输入新浪微博帐号"];
        [contentTextField setKeyboardType:UIKeyboardTypeDefault];
        [contentTextField setClearButtonMode:UITextFieldViewModeWhileEditing];
        [[contentTextField layer] setBorderColor:[[UIColor lightGrayColor] CGColor]];
        [[contentTextField layer] setBorderWidth:1.0];
        [[contentTextField layer] setCornerRadius:12];
        [contentTextField setClipsToBounds:YES];
        [self addSubview:contentTextField];
        
        UIImage* image = [UIImage imageNamed:@"btn_system2_normal.png"];
        
        cancelButton = [UIButton buttonWithType:UIButtonTypeCustom];
        cancelButton.frame = CGRectMake(8+TextFieldImage.size.width+5-10, 8, image.size.width/2, image.size.height/2);
        [cancelButton setBackgroundImage:[UIImage imageNamed:@"btn_system2_normal.png"] forState:UIControlStateNormal];
        [cancelButton setBackgroundImage:[UIImage imageNamed:@"btn_system2_press.png"] forState:UIControlStateSelected];
        [cancelButton setTitle:@"取消" forState:UIControlStateNormal];
        cancelButton.titleLabel.font = [UIFont boldSystemFontOfSize:16];
        cancelButton.tag = 199;
        [cancelButton addTarget:self action:@selector(buttonPress:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:cancelButton];
    }
    return self;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

- (void)buttonPress:(id)sender
{
    NSLog(@"buttonPress");
    [contentTextField setText:@""];
    [contentTextField setTextColor:[UIColor grayColor]];
    [delegate touchUpCancel];
}

#pragma -mark UITextFieldDelegate
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    if (textField == contentTextField)
    {
        [contentTextField setText:@""];
        [contentTextField setTextColor:[UIColor blackColor]];
    }

    return YES;
}

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
}

- (void)textFieldDidEndEditing:(UITextField *)textField
{
    [contentTextField resignFirstResponder];
    if ( textField == contentTextField && [contentTextField.text isEqualToString:@""])
    {
        [contentTextField setPlaceholder:@""];
        [contentTextField setTextColor:[UIColor grayColor]];
    }
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    if (1 == range.length) {//按下回格键
        return YES;
    }
    
    if ([string isEqualToString:@"\n"])
    {//按下return键
        //这里隐藏键盘，不做任何处理
        [textField resignFirstResponder];
        [delegate touchUpInsideImageViewCell:textField.text];
        return NO;
    }
    else
    {
        return YES;
    }
}

- (BOOL)textFieldShouldClear:(UITextField *)textField
{
    if (textField == contentTextField)
    {
        [contentTextField setText:@""];
        [contentTextField setPlaceholder:@""];
        [contentTextField setTextColor:[UIColor grayColor]];
    }
    return YES;
}

@end
